package com.sunyard.auth.controller;

import com.sunyard.auth.constants.Contants;
import com.sunyard.auth.constants.ErrorCode;
import com.sunyard.auth.exception.AuthException;
import com.sunyard.auth.model.AuthModel;
import com.sunyard.auth.redis.RedisKeyUtils;
import com.sunyard.auth.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @program auth
 * @description
 * @author raoinyx@163.com
 * @create 2020-07-18 00:23
 */
@Slf4j
public class BaseController {
	@Resource
	private RedisTemplate<Object, Object> redisTemplateCode;

	public String getSessionId(HttpServletRequest request) {
		return getIdByToken(request);
	}

	public String getToken(HttpServletRequest request) {
		return request.getHeader(Contants.TOKEN_KEY);
	}

	public String getIdByToken(HttpServletRequest request) {
		String token = request.getHeader(Contants.TOKEN_KEY);
		if (ObjectUtils.isEmpty(token)) {
			//如果用户ID为空，就直接报错
			throw new AuthException(ErrorCode.NOT_LOGINED);
		}
		//查询出authModel
		AuthModel auth = (AuthModel) redisTemplateCode.opsForValue().get(RedisKeyUtils.getTokenKey(token));
		if (ObjectUtils.isEmpty(auth)) {
			//如果用户ID为空，就直接报错
			throw new AuthException(ErrorCode.NOT_LOGINED);
		}
		return auth.getUserName();
	}

	public AuthModel getAuthModel(HttpServletRequest request) {
		String token = request.getHeader(Contants.TOKEN_KEY);
		if (ObjectUtils.isEmpty(token)) {
			//如果用户ID为空，就直接报错
			return new AuthModel();
		}
		return (AuthModel) redisTemplateCode.opsForValue().get(RedisKeyUtils.getTokenKey(token));
	}



	public String getTryIdByToken(HttpServletRequest request) {
		String token = request.getHeader(Contants.TOKEN_KEY);
		if (ObjectUtils.isEmpty(token)) {
			return "";
		}
		AuthModel auth = (AuthModel) redisTemplateCode.opsForValue().get(RedisKeyUtils.getTokenKey(token));
		if (ObjectUtils.isEmpty(auth)) {
			return "";
		}
		return auth.getUserName();
	}

	public String checkToken(HttpServletRequest request) {
		String token = request.getHeader(Contants.TOKEN_KEY);
		if (ObjectUtils.isEmpty(token)) {
			return "";
		}
		AuthModel auth = (AuthModel) redisTemplateCode.opsForValue().get(RedisKeyUtils.getTokenKey(token));
		if (ObjectUtils.isEmpty(auth)) {
			return "";
		}
		//token 做key,用户authModel做value
		String tokenCache = (String) redisTemplateCode.opsForValue().get(RedisKeyUtils.getAccountKey(auth.getUserName()));
		if (!ObjectUtils.isEmpty(tokenCache)) {
			redisTemplateCode.delete(RedisKeyUtils.getAccountKey(auth.getUserName()));
		}
		redisTemplateCode.delete(RedisKeyUtils.getTokenKey(token));
		//生成一个UUID
		String newToken = RandomUtil.getToken();
		AuthModel authModel = new AuthModel();
		authModel.setUserName(auth.getUserName());
		// id   token
		redisTemplateCode.opsForValue().set(RedisKeyUtils.getAccountKey(auth.getUserName()), newToken, Contants.TOKEN_TIME, TimeUnit.DAYS);
		//token model
		redisTemplateCode.opsForValue().set(RedisKeyUtils.getTokenKey(newToken), authModel, Contants.TOKEN_TIME, TimeUnit.HOURS);
		return newToken;
	}
}
